package com.example.service;

import com.example.mapper.OrderMapper;
import com.example.mapper.ProductMapper;
import com.example.mapper.ShoppingCarMapper;
import com.example.mapper.UserMapper;
import com.example.pojo.Order;
import com.example.pojo.Product;
import com.example.pojo.ShoppingCar;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.sql.Date;
import java.sql.Time;
import java.util.List;

/**
 * @Description:
 * @author: GaoPeng
 * @date: 2021/5/26 21:40
 */
@RequestMapping("/productService")
@RestController
@Service
@CrossOrigin
public class ProductService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private ObjectMapper jsonMapper;

    @Autowired
    private ShoppingCarMapper shoppingCarMapper;

    @Autowired
    private ProductMapper productMapper;

    /**
     * 添加商品
     * @param name 商品名称
     * @param price 单价
     * @param seller 卖家ID
     * @param stock 库存量
     * @return
     */
    @PostMapping("/addProduct")
    Boolean addProduct(@RequestParam("name")String name,@RequestParam("price")Float price,@RequestParam("seller")Integer seller,
                       @RequestParam("stock")Integer stock){
        Product product=new Product();
        product.setName(name);
        product.setPrice(price);
        product.setSeller(seller);
        product.setStock(stock);
        return productMapper.insert(product)>0;
    }

    /**
     * 获取所有商品信息
     * @return
     */
    @GetMapping("/getAllProducts")
    String getAllProducts(){
        try {
            return jsonMapper.writeValueAsString(productMapper.selectAll());
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 更新商品信息
     * @param productId 商品id
     * @param name 商品名
     * @param brief 商品简介
     * @return
     */
    @PostMapping("/updateProduct")
    Boolean updateProduct(@RequestParam("productId")Long productId,@RequestParam("productName")String name,@RequestParam("brief")String brief){
        return productMapper.updateProduct(productId,name,brief)>0;
    }

    /**
     * 删除商品
     * @param productId
     * @return
     */
    @GetMapping("/deleteProduct/{productId}")
    Boolean deleteProduct(@PathVariable Long productId){
        return productMapper.deleteProduct(productId)>0;
    }

    /**
     * 根据商品名查商品
     * @param name
     * @return
     */
    @GetMapping("/getProductsByName/{name}")
    String getProductsByName(@PathVariable String name){
        try {
            return jsonMapper.writeValueAsString(productMapper.selectByName(name));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }


}
